package cn.edu.heuet.bnve.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;


import java.util.ArrayList;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.databinding.ItemWeekBinding;

public class WeekAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<String> mDatas =new ArrayList<>();

    public ArrayList<String> getmDatas() {
        return mDatas;
    }

    public void setmDatas(ArrayList<String> mDatas) {
        this.mDatas = mDatas;
    }

    private int thisPosition = 1;

    //再定义一个int类型的返回值方法
    public int getThisPosition() {
        return thisPosition;
    }

    public WeekAdapter(Context context) {
        mContext = context;
        int i;
        for ( i=1;i<=19;i++){
            mDatas.add("第"+i+"周");
        }
    }
    //其次定义一个方法用来绑定当前参数值的方法
    //此方法是在调用此适配器的地方调用的，此适配器内不会被调用到
    public void setThisPosition(int thisPosition) {
        this.thisPosition = thisPosition;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_week, parent, false);
        return new NormalHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        NormalHolder normalHolder = (NormalHolder) holder;
        if (position == getThisPosition()) {
            normalHolder.itemView.setBackgroundResource(R.drawable.xui_config_color_edittext_touch);
            normalHolder.mTV.setText(mDatas.get(position));
        } else {
            normalHolder.itemView.setBackgroundResource(R.drawable.xui_config_color_edittext_normal);
            normalHolder.mTV.setText(mDatas.get(position));
        }

        //如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            normalHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.onItemClick(normalHolder.itemView, position);
                }
            });


        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class NormalHolder extends RecyclerView.ViewHolder {

        public TextView mTV;
        public NormalHolder(View itemView) {
            super(itemView);
            ItemWeekBinding binding = DataBindingUtil.bind(itemView);
            mTV = binding.weekNumber;

        }

    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }



}